import { defineStore } from 'pinia'
import { ref, shallowRef } from 'vue'
import { getRecommend, getRecommendNewSong, getTopListData } from '@/api/song'
import type { Personalized, NewSongResult, TopList } from '@/types/song'
import { useRouter } from 'vue-router'

export const useMusicStore = defineStore('music', () => {
  const router = useRouter()

  // 推荐歌单
  const personalizedList = ref<Array<Personalized>>([])
  const getRecommendList = async () => {
    const { data } = await getRecommend()
    if (data.code !== 200) return ElMessage.error('获取失败')
    personalizedList.value = data.result
  }

  // 推荐新音乐
  const newMusicList = ref<Array<NewSongResult>>([])
  const getNewMusicList = async () => {
    const { data } = await getRecommendNewSong()
    if (data.code !== 200) return ElMessage.error('获取失败')
    newMusicList.value = data.result
  }

  // 跳转到专属歌单的详情页
  const toExclusivePlaylist = () => {
    router.push({
      name: 'exclusivePlaylist',
    })
  }

  // 音乐馆tab
  const tabList = shallowRef([
    {
      label: '精选',
      value: 'selected',
    },
    {
      label: '排行',
      value: 'ranking',
    },
    {
      label: '歌手',
      value: 'artist',
    },
    {
      label: '分类歌单',
      value: 'playlist',
    },
    {
      label: '有声电台',
      value: 'audioStation',
    },
    {
      label: '数字专辑',
      value: 'digitalAlbum',
    },
  ])
  const activeName = ref<string>('selected')
  const changeTab = (value: any) => {
    activeName.value = value
    router.push({ name: value })
  }
  // 获取榜单摘要
  const topList = ref<Array<TopList>>([])
  const getTopList = async () => {
    const { data } = await getTopListData()
    if (data.code !== 200) return ElMessage.error('获取失败')
    topList.value = data.list
  }

  return {
    personalizedList,
    getRecommendList,
    newMusicList,
    getNewMusicList,
    toExclusivePlaylist,
    tabList,
    activeName,
    changeTab,
    getTopList,
    topList,
  }
})
